﻿Imports System.ComponentModel
Imports System.Net
Public Class PictureUploader

    'Dim oFd As New OpenFileDialog
    Dim odd As New FolderBrowserDialog
    Dim log As String = ""



    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As System.ComponentModel.BackgroundWorker = TryCast(sender, System.ComponentModel.BackgroundWorker)
        e.Result = uploader(TryCast(e.Argument, System.Collections.ObjectModel.ReadOnlyCollection(Of String)), worker, e)

    End Sub

    Private Function uploader(ByVal _files As System.Collections.ObjectModel.ReadOnlyCollection(Of String), ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As DoWorkEventArgs) As String
        Dim ftpCon As New FtpConnector
        ftpCon.showProgress = False

        Dim i As Integer = 0
        Do While (i < _files.Count)
            If worker.CancellationPending Then   'check first if user cancelled the process
                e.Cancel = True
            Else
                Dim fileName As String = _files(i).Remove(0, _files(i).LastIndexOf(CChar("\")) + 1)
                'ftpCon.upload(_files(i), fileName, "ftp://tecnikk.co.uk/httpdocs/media/import/")
                'ftpCon.upload(_files(i), , )
                ftpCon.upload(_files(i), My.Settings.ftp_site, My.Settings.ftp_media_dir, fileName)

                log = ftpCon.getResponse & " ->" & _files(i).ToString
                If ftpCon.getResponse = "OK" Then

                    Try
                        If My.Settings.uploader_moveFiles Then
                            If My.Computer.FileSystem.DirectoryExists(My.Settings.uploader_moveTo) Then
                                My.Computer.FileSystem.MoveFile(_files(i), My.Settings.uploader_moveTo & "\" & fileName, True)
                            Else
                                If My.Computer.FileSystem.DirectoryExists(My.Settings.local_workPath & "\" & "up\") Then
                                    My.Computer.FileSystem.MoveFile(_files(i), My.Settings.local_workPath & "\" & "up\")
                                Else
                                    My.Computer.FileSystem.CreateDirectory(My.Settings.local_workPath & "\" & "up\")
                                    My.Computer.FileSystem.MoveFile(_files(i), My.Settings.local_workPath & "\" & "up\")
                                End If
                            End If
                        End If
                    Catch ex As Exception
                        MessageBox.Show("Unexpected Error in Moving Files or Creating Directory: " & ex.Message)
                    Finally
                        worker.ReportProgress(1) 'just force a screen update no progress bar
                        i += 1
                    End Try
                Else
                    log = ftpCon.getResponse & " R->" & _files(i).ToString
                    worker.ReportProgress(1) 'just force a screen update no progress bar
                End If


            End If
        Loop
        log = "All Complete..."

        Return log
    End Function

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        'Me.ProgressBar1.Value = e.ProgressPercentage
        ListBoxLog.Items.Add(log)
        ListBoxLog.SelectedIndex = ListBoxLog.Items.Count - 1

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If Not (e.Error Is Nothing) Then
            MessageBox.Show(e.Error.Message)
        ElseIf e.Cancelled Then
            MessageBox.Show("Cancelled")
        Else
            'result
            ListBoxLog.Items.Add(log)
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelToolStripButton.Click
        BackgroundWorker1.CancelAsync()
    End Sub

    Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click


        If My.Computer.FileSystem.DirectoryExists(My.Settings.uploader_moveTo) Then
            odd.SelectedPath = My.Settings.uploader_baseDir
        Else
            odd.SelectedPath = My.Settings.local_workPath
        End If


        If odd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim directory As String
            directory = odd.SelectedPath
            Dim myFiles As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            myFiles = My.Computer.FileSystem.GetFiles(directory, FileIO.SearchOption.SearchTopLevelOnly, "*.*")

            BackgroundWorker1.RunWorkerAsync(myFiles)
        End If
    End Sub

End Class